package com.Guigu.AIchat.service;

import com.Guigu.AIchat.domain.ChatMessage;
import com.Guigu.AIchat.inter.ChatMessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

// ... existing code ...
@Service
public class AIChatService {
//    @Autowired
//    private AIChatRepository repository;

    @Autowired
    private ChatMessageRepository repository;

    @Autowired
    private DeepSeekService deepSeekService;
//    public ChatMessage handleMessage(ChatMessage message) {
//        // 这里可接入AI服务并处理图片
//        message.setTimestamp(LocalDateTime.now());
//        repository.save(message);
//        return message;
//    }
    public ChatMessage handleMessage(ChatMessage message) {
        // 保存用户消息
        message.setTimestamp(LocalDateTime.now());
        repository.save(message);

        // 调用DeepSeek获取回复
        String aiResponse = deepSeekService.getChatResponse(message.getText());

        // 创建AI回复消息
        ChatMessage aiMessage = new ChatMessage();
        aiMessage.setUserId(message.getUserId()); // AI用户ID
        aiMessage.setText(aiResponse);
        aiMessage.setTimestamp(LocalDateTime.now());
        aiMessage.setImageUrl(message.getImageUrl()); // 保留图片链接

        // 保存AI回复
        repository.save(aiMessage);

        return aiMessage;
    }

    public List<ChatMessage> getHistory(Long userId) {
        return repository.findByUserIdOrderByTimestampDesc(userId);
    }
}
// ... existing code ...